//
//  SEPrescriptionPickDrugsListView.swift
//  SmartEyeForDoctor
//
//  Created by QDSG on 2021/5/11.
//

import UIKit

class SEPrescriptionPickDrugsListView: UIView {
    
    var viewModel = SEPrescriptionDrugPickViewModel()
    
    var selectedHandler: (() -> Void)?
    var dismissHandler: (() -> Void)?
    
    @IBOutlet private weak var searchBar: UISearchBar! {
        didSet { searchBar.delegate = self }
    }

    @IBOutlet private weak var tableView: UITableView! {
        didSet {
            tableView.backgroundColor = .white
            tableView.tableFooterView = UIView()
            tableView.estimatedRowHeight = 60
            tableView.rowHeight = UITableView.automaticDimension
            tableView.delegate = self
            tableView.dataSource = self
            
            let header = SERefreshHeader { [weak self] in self?.loadData() }
            header.lastUpdatedTimeLabel?.isHidden = true
            header.stateLabel?.isHidden = true
            tableView.se_header = header
            tableView.se_header?.beginRefreshing()
            tableView.se_footer = SERefreshFooter { [weak self] in self?.loadData(true) }
            
            tableView.register(SEPrescriptionPickDrugsListCell.nibForCell(),
                               forCellReuseIdentifier: SEPrescriptionPickDrugsListCell.reuseIdentifier())
        }
    }
    
    @IBAction private func dismissAction() {
        searchBar.resignFirstResponder()
        dismissHandler?()
    }
    
    private func loadData(_ more: Bool = false) {
        viewModel.getListOfPrescriptionDrug(more)
        viewModel.updateData = { [weak self] status in
            self?.endRefreshing()
            self?.tableView.reloadData()
        }
    }
    
    private func loadDetailOfDrug(with vm: SEPrescriptionDrugPickViewModel) {
        viewModel.getDetailOfPrescriptionDrug(id: vm.drugPickModel.id ?? "")
        viewModel.loadDetailOfDrugHandler = { [weak self] status in
            switch status {
            case .success:
                self?.selectedHandler?()
            default:
                break
            }
        }
    }
    
    private func endRefreshing() {
        tableView.endHeaderRefreshing()
        tableView.endFooterRefreshing(hasMore: viewModel.hasMore)
    }
}

extension SEPrescriptionPickDrugsListView: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.numberOfRows
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SEPrescriptionPickDrugsListCell.reuseIdentifier(),
                                                 for: indexPath) as! SEPrescriptionPickDrugsListCell
        cell.update(with: viewModel.viewModel(at: indexPath))
        return cell
    }
}

extension SEPrescriptionPickDrugsListView: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vm = viewModel.viewModel(at: indexPath)
        loadDetailOfDrug(with: vm)
    }
}

extension SEPrescriptionPickDrugsListView: UISearchBarDelegate {
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
        guard let content = searchBar.text?.trimmingCharacters(in: .whitespacesAndNewlines),
              !content.isEmpty else { return }
        viewModel.content = content
        loadData()
    }
}
